/*
 * SubSonic - http://subsonicproject.com
 * 
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an 
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
*/

using System;

namespace SubSonic.QueryEngine
{
    /// <summary>
    /// Summary for the Select class
    /// </summary>
    public class Select : SqlQuery
    {
        /// <summary>
        /// Sets the query.
        /// </summary>
        /// <param name="schema">The schema.</param>
        private void SetQuery(TableSchema.Table schema)
        {
            string sqlStatement = GetSqlParser(schema.Provider.Name).SelectTemplate();
            Command = new QueryCommand(GetSqlParser(schema.Provider.Name).GetSelect(schema));
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Select"/> class.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        public Select(string tableName)
        {
            ExecuteSelect(tableName, String.Empty);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Select"/> class.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="providerName">Name of the provider.</param>
        public Select(string tableName, string providerName)
        {
            ExecuteSelect(tableName, providerName);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Select"/> class.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="providerName">Name of the provider.</param>
        /// <param name="columnList">The column list.</param>
        public Select(string tableName, string providerName, string columnList)
        {
            ColumnList = columnList;
            ExecuteSelect(tableName, providerName);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Select"/> class.
        /// </summary>
        /// <param name="schema">The schema.</param>
        public Select(TableSchema.Table schema)
        {
            ExecuteSelect(schema);
        }

        /// <summary>
        /// Executes the select.
        /// </summary>
        /// <param name="schema">The schema.</param>
        private void ExecuteSelect(TableSchema.Table schema)
        {
            if(!Tables.Contains(schema))
                Tables.Add(schema);
            SetQuery(schema);
        }

        /// <summary>
        /// Executes the select.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="providerName">Name of the provider.</param>
        private void ExecuteSelect(string tableName, string providerName)
        {
            TableSchema.Table schema = DataService.GetSchema(tableName, providerName);
            ExecuteSelect(schema);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Select"/> class.
        /// </summary>
        /// <param name="schema">The schema.</param>
        /// <param name="columnList">The column list.</param>
        public Select(TableSchema.Table schema, string columnList)
        {
            ColumnList = columnList;
            SetQuery(schema);
        }

        /// <summary>
        /// Distincts this instance.
        /// </summary>
        /// <returns></returns>
        public SqlQuery Distinct()
        {
            IsDistinct = true;
            return this;
        }
    }
}